//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    DummyRulesLoader.cs
//
// Abstract:
//    
//    Dummy rules loader for testing.
//
// Classes:
//
//    Microsoft.EnhancedReminders.OutlookAddIn.UnitTests.DummyRulesLoader
//
//--

using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders.OutlookAddIn.UnitTests
{
    /// <summary>
    /// Implements IRulesLoader for unit testing.
    /// </summary>
    public class DummyRulesLoader : IRulesLoader
    {
        private NotificationRules _storedRules; // The rules to load from "storage".

        /// <summary>
        /// Constructor.
        /// </summary>
        public DummyRulesLoader()
            : this(false)
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="populateRules">True to populate the rules list, false to leave it empty.</param>
        public DummyRulesLoader(bool populateRules)
        {
            // Create the rules which will be loaded into the rules viewer when Load() is called.
            NotificationRules rules = new NotificationRules();

            if (populateRules) // Add some rules to make testing more convenient.
            {
                rules.Add(CreateRule("First voicemail rule"));
                rules.Add(CreateRule("Second voicemail rule"));
                rules.Add(CreateRule("Third voicemail rule"));
            }

            Initialize(rules);
        }

        /// <summary>
        /// Helper method to create a dummy rule with the time condition selected.
        /// </summary>
        private NotificationRuleItem CreateRule(string ruleName)
        {
            NotificationRuleItem rule = new NotificationRuleItem(MailboxType.Voicemail, ruleName, DateTime.Now, Microsoft.EnhancedReminders.UnitTests.Helpers.CreateDummyChannels());
            Assert.IsNotNull(rule.Conditions, "rule.Conditions");
            Assert.IsTrue(rule.Conditions.Count > 0, "rule.Conditions.Count");

            NotificationTimeCondition timeCondition = Helpers.AssertCast<NotificationTimeCondition>(rule.Conditions[0]);
            timeCondition.IsSelected = true;

            Assert.IsNotNull(timeCondition.TimeArray, "timeCondition.TimeArray");
            Assert.IsTrue(timeCondition.TimeArray.Count > 0, "timeCondition.TimeArray.Count");

            // Select a cell in the time grid to make the time condiiton valid.
            timeCondition.TimeArray[0] = true;

            return rule;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="rules">A set of "stored" rules provided by the unit test.</param>
        public DummyRulesLoader(NotificationRules rules)
        {
            Initialize(rules);
        }
 
        /// <summary>
        /// Initializes the rules loader.
        /// </summary>
        /// <param name="rules">The "stored" rules to be loaded by the rules viewer.</param>
        private void Initialize(NotificationRules rules)
        {
            Debug.Assert(rules != null, "rules");

            _storedRules = rules;
        }

        /// <summary>
        /// Retrieves the stored rules.
        /// </summary>
        public NotificationRules StoredRules
        {
            get
            {
                return _storedRules;
            }
        }

        /// <summary>
        /// Implements IRulesLoader.Load()
        /// </summary>
        public void Load(NotificationRules rules)
        {
            Assert.IsNotNull(rules, "rules");
            Assert.AreEqual(0, rules.Count, "rules.Count");

            foreach (NotificationRuleItem rule in _storedRules)
            {
                rules.Add(rule);
            }

            rules.DoNotPromptUserOnDelete = true; // Suppress delete rule warning dialog for DRTs.
        }

        /// <summary>
        /// Implements IRulesLoader.Save()
        /// </summary>
        /// <param name="rules"></param>
        public void Save(NotificationRules rules)
        {
            throw new NotImplementedException();
        }
    }
}
